﻿using System;
using CardLogistics.Model.Purchase;
using System.Data.OracleClient;
using System.Data;

namespace CardLogistics.DAL.Purchase
{
    /// <summary>
    /// 采购需求详细信息数据访问类
    /// </summary>
    public class Purchase_DetailedData
    {
        #region 查询
        /// <summary>
        /// 根据条件查询相应的表的字段
        /// </summary>
        /// <param name="StrWhere">查询条件</param>
        /// <param name="StrFields">要查询的字段</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetList(string StrWhere, string StrFields)
        {
            return CommonData.GetListOrderBy(StrWhere, "v_purchase_detailed", StrFields, "", 0);
        }

        /// <summary>
        /// 根据编号查询基本信息
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetInfoById(string id)
        {
            return GetList(" ID = '" + id + "' ", "*");
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Purchase_Detailed GetModel(string rId)
        {
            Purchase_Detailed model = new Purchase_Detailed();
            DataRow row = GetInfoById(rId).Rows[0];
            if (row != null)
            {
                if (row["ID"] != null)
                {
                    model.ID = row["ID"].ToString();
                }
                if (row["DEMANDID"] != null)
                {
                    model.DEMANDID = row["DEMANDID"].ToString();
                }
                if (row["CARDTYPE"] != null)
                {
                    model.CARDTYPE = row["CARDTYPE"].ToString();
                }
                if (row["CARDNUMBER"] != null && row["CARDNUMBER"].ToString() != "")
                {
                    model.CARDNUMBER = int.Parse(row["CARDNUMBER"].ToString());
                }
                if (row["LASTUPDATEUSER"] != null)
                {
                    model.LASTUPDATEUSER = row["LASTUPDATEUSER"].ToString();
                }
                if (row["LASTUPDATEDATE"] != null && row["LASTUPDATEDATE"].ToString() != "")
                {
                    model.LASTUPDATEDATE = DateTime.Parse(row["LASTUPDATEDATE"].ToString());
                }
                if (row["ISDELETE"] != null && row["ISDELETE"].ToString() != "")
                {
                    model.ISDELETE = int.Parse(row["ISDELETE"].ToString());
                }

            }
            return model;
        }
        #endregion

        #region 通过采购编号ID获取需求信息

        #region 通过采购编号Id获取查询信息
        /// <summary>
        /// 通过采购编号Id获取查询信息
        /// </summary>
        /// <param name="demandId">采购编号Id</param>
        /// <returns></returns>
        public DataTable GetInfoByDemandId(string demandId)
        {
            return GetList("DemandId = '" + demandId + "' ", "*");
        }
        #endregion

        #region 通过采购编号ID得到一个对象实体
        /// <summary>
        /// 通过采购编号ID得到一个对象实体
        /// </summary>
        public Purchase_Detailed GetDetailModel(string demandId)
        {
            Purchase_Detailed model = new Purchase_Detailed();
            DataRow row = GetInfoByDemandId(demandId).Rows[0];
            if (row != null)
            {
                if (row["ID"] != null)
                {
                    model.ID = row["ID"].ToString();
                }
                if (row["DEMANDID"] != null)
                {
                    model.DEMANDID = row["DEMANDID"].ToString();
                }
                if (row["CARDTYPE"] != null)
                {
                    model.CARDTYPE = row["CARDTYPE"].ToString();
                }
                if (row["CARDNUMBER"] != null && row["CARDNUMBER"].ToString() != "")
                {
                    model.CARDNUMBER = int.Parse(row["CARDNUMBER"].ToString());
                }
                if (row["LASTUPDATEUSER"] != null)
                {
                    model.LASTUPDATEUSER = row["LASTUPDATEUSER"].ToString();
                }
                if (row["LASTUPDATEDATE"] != null && row["LASTUPDATEDATE"].ToString() != "")
                {
                    model.LASTUPDATEDATE = DateTime.Parse(row["LASTUPDATEDATE"].ToString());
                }
                if (row["ISDELETE"] != null && row["ISDELETE"].ToString() != "")
                {
                    model.ISDELETE = int.Parse(row["ISDELETE"].ToString());
                }

            }
            return model;
        }
        #endregion

        #endregion

        #region 新增
        /// <summary>
        /// 新增信息
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>返回插入数据的编号</returns>
        public string Add(Purchase_Detailed model)
        {
            try
            {
                OracleParameter[] parameters = {
                    new OracleParameter("v_varOut", OracleType.VarChar,50),
                    new OracleParameter("v_demandid", OracleType.VarChar,50),
                    new OracleParameter("v_cardtype", OracleType.VarChar,50),
                    new OracleParameter("v_cardnumber", OracleType.Number),
                    new OracleParameter("v_lastupdateuser", OracleType.VarChar,50),
                    new OracleParameter("v_lastupdatedate", OracleType.DateTime),
                    new OracleParameter("v_isdelete", OracleType.Number)};
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = model.DEMANDID;
                parameters[2].Value = model.CARDTYPE;
                parameters[3].Value = model.CARDNUMBER;
                parameters[4].Value = model.LASTUPDATEUSER;
                parameters[5].Value = model.LASTUPDATEDATE;
                parameters[6].Value = model.ISDELETE;
                string StrOutPar = DbHelperSQL.RunProcedure("P_purchase_detailed_ADD", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                return "err," + ex.Message;
            }
        }
        #endregion

        #region 修改
        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>返回插入数据的编号</returns>
        public string Update(Purchase_Detailed model)
        {
            try
            {
                OracleParameter[] parameters = {
                    new OracleParameter("v_varOut", OracleType.VarChar,50),
                    new OracleParameter("v_id", OracleType.VarChar,50),
                    new OracleParameter("v_demandid", OracleType.VarChar,50),
                    new OracleParameter("v_cardtype", OracleType.VarChar,50),
                    new OracleParameter("v_cardnumber", OracleType.Number),
                    new OracleParameter("v_lastupdateuser", OracleType.VarChar,50),
                    new OracleParameter("v_lastupdatedate", OracleType.DateTime),
                    new OracleParameter("v_isdelete", OracleType.Number)};
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = model.ID;
                parameters[2].Value = model.DEMANDID;
                parameters[3].Value = model.CARDTYPE;
                parameters[4].Value = model.CARDNUMBER;
                parameters[5].Value = model.LASTUPDATEUSER;
                parameters[6].Value = model.LASTUPDATEDATE;
                parameters[7].Value = model.ISDELETE;
                string StrOutPar = DbHelperSQL.RunProcedure("P_purchase_detailed_UPDATE", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                return "err," + ex.Message;
            }
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="id">删除信息的ID</param>
        /// <returns>删除成功返回True。否则返回false</returns>
        public string Delete(string id)
        {
            try
            {
                OracleParameter[] parameters = {
                        new OracleParameter("VarOut",  OracleType.VarChar,100),
					    new OracleParameter("v_id",  OracleType.VarChar,50)
                };
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = id;
                string StrOutPar = DbHelperSQL.RunProcedure("P_purchase_detailed_Delete", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}
